home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / gui / gui4cli.lha / Gui4Cli / Ext / Exmpl_src / 1_GCTalk / GCTalk.c < prev    next >
C/C++ Source or Header  |  1998-10-22  |  4KB  |  129 lines

  1. /****************************************************************
  2. **
  3. **    GCTalk.c by dck@hol.gr
  4. **
  5. **    This little program is to show you how to talk to 
  6. **    Gui4Cli. All it does is to get a lock on the main
  7. **    Gui4Cli structure and print out the names of all
  8. **    the currently loaded guis and then a simple command 
  9. **    to Gui4Cli for execution.
  10. **
  11. **    There is an other version of this called GCTalkNS.c
  12. **    (in this dir) which uses the SAS "nostartup" option 
  13. **    to produce an even smaller executable.
  14. **
  15. ******************************************************************/
  16. // some of these may not be needed..
  17. #include <exec/exec.h>
  18. #include <exec/execbase.h>
  19. #include <exec/memory.h>
  20. #include <dos/dosextens.h>
  21. #include <dos/rdargs.h>
  22. #include <dos/dostags.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <dos.h>
  27. #include <proto/dos.h>
  28. #include <proto/exec.h>
  29. #include <graphics/text.h>
  30.  
  31. // Gui4Cli.h include file (assuming it's in the include: dir)
  32. #include <Gui4Cli.h>
  33.  
  34. // prototype
  35. int sendmsg (UBYTE *, struct g4cmsg *);
  36.  
  37. // ==============================================================
  38. // MAIN - print the names of all loaded guis
  39. // ==============================================================
  40.  
  41. LONG main (void)
  42. {
  43.     struct Process *myself = (struct Process *)FindTask(NULL);
  44.     struct MsgPort *myport;
  45.  
  46.     struct g4cmsg  msg;    // the message we'll use
  47.     struct GCmain  *gc;    // Gui4Cli's main structure
  48.     struct guifile *gf;    // a gui file pointer
  49.     UBYTE  command[100];    // a temporary buffer
  50.     LONG   ret = 10;    // our return code
  51.       
  52.     // this is our proccess's message port
  53.     myport = &myself->pr_MsgPort;
  54.  
  55.     // clear & set up the message structure
  56.     memset ((char *)&msg, 0, sizeof(msg));
  57.     msg.node.mn_ReplyPort = myport;
  58.     msg.node.mn_Length = sizeof(struct g4cmsg);
  59.     msg.magic = 392001; // so that g4c recognises us
  60.  
  61.     // make it a lock message
  62.     msg.type = GM_LOCK;
  63.  
  64.     // send the message - a return of 0 means ok
  65.     // otherwise there was a failure and we exit indicating error
  66.     if (ret = sendmsg ("Gui4Cli", &msg)) return (ret);
  67.  
  68.     // The pointer to Gui4Cli's main structure, is in msg.gcmain
  69.     gc = msg.gcmain;
  70.  
  71.     // make sure that it's valid..
  72.     if (gc && (gc->magic == MM_G4C))
  73.     {
  74.         // print out the names of all the guis currently loaded
  75.         for (gf = gc->topguifile; gf; gf = gf->next)
  76.              Printf ("FILE: %s\n", gf->name);
  77.     }
  78.  
  79.     // *must* unlock Gui4Cli after we're through using it..
  80.     msg.type = GM_UNLOCK;
  81.     sendmsg ("Gui4Cli", &msg);
  82.  
  83.     // now for my next trick...
  84.     // we send a command to Gui4Cli to execute..
  85.     strcpy (command, "SETVAR *GlobVar \'Some value\'");
  86.     msg.com  = command;
  87.     msg.type = GM_COMMAND;
  88.     ret = sendmsg ("Gui4Cli", &msg);
  89.     // now global var *GlobVar will contain the words "Some value" 
  90.  
  91.     // that's all..
  92.     return (ret);
  93. }
  94.  
  95.  
  96. // ==============================================================
  97. // Send a message, wait for reply.. 
  98. // -->> return 0 for OK or error code otherwise
  99. // - portname = the name of Gui4Cli message port (usually Gui4Cli)
  100. // - msg = the message we received
  101. // ==============================================================
  102.  
  103. sendmsg (UBYTE *portname, struct g4cmsg *msg)
  104. {
  105.     struct MsgPort *gcport, *myport;
  106.     
  107.     // get a pointer to our message port (for code clarity)
  108.     myport = msg->node.mn_ReplyPort;
  109.  
  110.     Forbid();
  111.     if (gcport = FindPort(portname))
  112.     {
  113.        PutMsg (gcport, &msg->node);
  114.        Permit();
  115.        // wait for reply..
  116.        WaitPort (myport);
  117.        msg = (struct g4cmsg *)GetMsg (myport);
  118.        // return Gui4Cli's return code (probably 0 meaning OK)
  119.        return (msg->res);
  120.     }
  121.     Permit();
  122.     PutStr ("Could not find port!\n"); 
  123.     return (10);
  124. }
  125.  
  126.  
  127.  
  128.  
  129.